package com.cfk.shopmail.gateway.config;

/*
    @CreateTime 2021/11/23 10:00
    @CreateBy cfk
*/

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

@Configuration
public class ShopmailCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        //想要配置CorsConfigurationSource，发现此配置为接口类，通过其实现类UrlBasedCorsConfigurationSource来实现
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configuration = new CorsConfiguration();

        //配置跨域
        configuration.addAllowedHeader("*"); //允许什么请求头跨域
        configuration.addAllowedMethod("*"); //允许什么请求方法跨域
        configuration.addAllowedOrigin("*"); //允许什么请求来源跨域
        configuration.setAllowCredentials(true); //是否允许cookie跨域

        source.registerCorsConfiguration("/**", configuration); //对所有的路径都进行跨域处理
        return new CorsWebFilter(source);
    }
}
